home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / games / gripple / gripple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-03  |  4.2 KB  |  175 lines

  1. /* GRIPPLE.C -- a 4x4 peg game. Requires VGA.
  2.    The file EGAVGA.BGI must be in the same directory
  3.    as the executable file.  */
  4.  
  5. #include <graphics.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <time.h>
  10.  
  11. #define PEGCOLOR1 1
  12. #define PEGCOLOR2 2
  13. #define PEGCOLOR3 14
  14. #define PEGCOLOR4 4
  15. #define SELECT_ON_EXEC 5
  16.  
  17. void grstart(void);
  18. void Startgame(void);
  19. void RandomBoard(void);
  20. void DrawFour(int,int);
  21. void DoRect(int,int);
  22. void Rotate(int,int);
  23. void Shuffle(void);
  24. void WinCheck(void);
  25.  
  26. int kb,olds=5,s,shuf,c[5][5],count=0;
  27. char cou[4];
  28.  
  29. void main(void)
  30. {
  31. grstart();
  32. Startgame();
  33. do {kb=getch();
  34. switch (kb)
  35. {    
  36.     case 0: kb=getch();
  37.         switch(kb)
  38.         {case 75: case 72: Rotate(s,1); break;
  39.          case 77: case 80: Rotate(s,2); break;}
  40.          break;
  41.     case 49: case 50: case 51: case 52: case 53:
  42.         DoRect(kb-48,15); break;
  43.     case 18: Shuffle(); break;
  44. } WinCheck(); 
  45. } while (kb!=27);
  46. closegraph();   
  47. }
  48.  
  49.  
  50. void grstart(void)
  51. {
  52.    int gdriver = DETECT, gmode, errorcode;
  53.    initgraph(&gdriver, &gmode, "");
  54.    errorcode = graphresult();
  55.    if (errorcode != grOk)
  56.    {
  57.       printf("Graphics error: %s\n", grapherrormsg(errorcode));
  58.       printf("Press any key to halt:");
  59.       getch();
  60.       exit(1);  }
  61. }
  62.  
  63. void Startgame(void)
  64. {
  65. randomize();
  66. setcolor(1);
  67. setfillstyle(1,4);
  68. rectangle(205,95,445,335);
  69. rectangle(324,95,326,335);
  70. rectangle(205,214,445,216);    
  71. rectangle(270,160,380,270);
  72. setcolor(8);
  73. rectangle(25,15,615,50);
  74. rectangle(1,1,639,450);
  75. rectangle(139,355,501,435);
  76. setcolor(15);
  77. outtextxy(255,148,"1");
  78. outtextxy(255,280,"3");
  79. outtextxy(387,148,"2");
  80. outtextxy(387,280,"4");
  81. outtextxy(322,212,"5");
  82. settextstyle(0,0,2);
  83. outtextxy(270,20,"Gripple");
  84. settextstyle(0,0,1);
  85. outtextxy(32,40,"Based on the game by M-Squared Incorporated.  Program by Doug Beeferman.");
  86. outtextxy(280,360,"Key Commands:");
  87. outtextxy(160,380,"1,2,3,4,5:  Select a region to rotate");
  88. outtextxy(152,390,"Left Arrow:  Rotate region counterclockwise");
  89. outtextxy(144,400,"Right Arrow:  Rotate region clockwise");
  90. outtextxy(184,410,"CTRL-R:  Reshuffle the pieces");
  91. outtextxy(208,420,"ESC:  Quit the program");
  92. s=SELECT_ON_EXEC;
  93. Shuffle();
  94. DoRect(s,15);
  95. }
  96.  
  97. void DrawFour(int x,int y)
  98. {int i,j;
  99. for (i=y; i<y+2; i++)   
  100. for (j=x; j<x+2; j++)
  101.     {setfillstyle(1,c[j][i]);
  102.     fillellipse(175+60*j,65+i*60,20,20);}
  103. setfillstyle(1,0);
  104. bar(600,5,635,13);
  105. setcolor(15);
  106. itoa(count,cou,10);
  107. outtextxy(600,6,cou);
  108. }
  109.  
  110. void DoRect(int x,int color)
  111. {
  112. setcolor(color);
  113. if (color==15) {
  114. olds=s; s=x; 
  115. if (olds!=s) {DoRect(olds,1);
  116.         setcolor(15); outtextxy(322,212,"5");}
  117. }
  118. switch (x) {
  119.     case 1: rectangle(205,95,324,214); break;
  120.     case 2: rectangle(326,95,445,214); break;
  121.     case 3: rectangle(205,216,324,335); break;
  122.     case 4: rectangle(326,216,445,335); break;
  123.     case 5: rectangle(270,160,380,270); break;
  124.        }
  125. }
  126.  
  127. void Rotate(int x,int dir)
  128. {int a,b,i,j,temp;
  129. count++;
  130. switch(x) {
  131.     case 1: case 2: a=-1+2*x; b=1; break;
  132.     case 3: case 4: a=-5+2*x; b=3; break;
  133.     case 5: a=2; b=2; break;}
  134. temp=c[a][b];
  135. if (dir==1) {c[a][b]=c[a+1][b]; c[a+1][b]=c[a+1][b+1];
  136.     c[a+1][b+1]=c[a][b+1]; c[a][b+1]=temp;}
  137. else {c[a][b]=c[a][b+1]; c[a][b+1]=c[a+1][b+1];
  138.     c[a+1][b+1]=c[a+1][b]; c[a+1][b]=temp;} 
  139. if (!shuf) DrawFour(a,b); 
  140. }
  141.  
  142. void Shuffle(void)
  143. {int i,j,r;
  144. shuf=1;
  145. for (i=1; i<3; i++)   
  146. for (j=1; j<3; j++)
  147. {c[j][i]=PEGCOLOR1; c[j+2][i]=PEGCOLOR2; 
  148. c[j][i+2]=PEGCOLOR3; c[j+2][i+2]=PEGCOLOR4;}
  149. for (i=1; i<201; i++)
  150.     {r=random(7)+1; if (r>5) r=5; Rotate(r,1);}
  151. shuf=0; count=0;
  152. DrawFour(1,1); DrawFour(3,1); DrawFour(1,3); DrawFour(3,3);
  153.  
  154. }
  155.  
  156. void WinCheck(void)
  157. {int i,j,win=1;
  158. if (c[1][1]==c[2][2])
  159.     { for (i=1; i<4; i+=2) for (j=1; j<4; j+=2)
  160.      if (((c[j][i]!=c[j][i+1])||(c[j][i+1]!=c[j+1][i+1]))||
  161.         (c[j+1][i+1]!=c[j+1][i])) win=0;  }
  162. else win=0;
  163. if (win==1) {for (i=1; i<6; i++) {
  164.     setcolor(15); rectangle(1,1,639,450); delay(250);
  165.     setcolor(8);  rectangle(1,1,639,450); delay(250); }
  166.         setcolor(15);
  167.     setfillstyle(1,0); bar(32,20,605,49);
  168.     settextstyle(0,0,2); outtextxy(193,20,"Congratulations!");
  169.     settextstyle(0,0,1); outtextxy(233,40,"Press a key to continue.");
  170.     i=getch(); setfillstyle(1,0); bar(32,20,605,50);
  171.     Startgame();
  172.     }
  173.  
  174.  
  175. }